import json
import requests
def hex_decrypt(hex_str:str):
    """解密函数"""
    num = int(hex_str, 16)
    return num + 9538
def hex_encrypt(dec_num:int):
    """加密函数"""
    original = dec_num - 9538
    return format(original, '03x')  # 转为三位小写，补前导零
def tft_decrypt_to_list(tft_hexstr='023142ed2ff2f42e930c2fe2f731f000TFTSet14'):
    '''
    解密tft小队字符串 返回英雄id列表
    :return: int
    '''
    hexstr = tft_hexstr[2:-8].strip()
    #将hexstr 3个为一个词 分割
    id_list = [hexstr[i:i+3] for i in range(0, len(hexstr), 3) if hexstr[i:i+3]!="000"]
    id_list = [hex_decrypt(item) for item in id_list]
    return id_list
def tft_encrypt_to_str(id_list=list):
    '''
    解密tft小队字符串 返回英雄id列表
    :return: int
    '''
    hexstr_start="02"
    hexstr_end = "TFTSet14"
    # id_list 补足10个成员 补足的都用000填充后面的成员
    hex_list=[ ]
    for item in id_list:
        if item==10348:
            item=11356
        hex_list.append(hex_encrypt(item))
    #补足10个成员
    target_length = 10
    fill_value = "000"
    missing_elements = target_length - len(hex_list)
    # 使用 extend() 方法补充
    hex_list.extend([fill_value] * missing_elements)
    hexstr=hexstr_start+"".join(hex_list)+hexstr_end
    return hexstr
class TFT():#云顶攻略类 单列模式
    def __init__(self):
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 Edg/108.0.1462.42'}
        self.saiji = "s14"  # 赛季
        self.strategy_list=self.get_strategy_list()#攻略列表 包含 所有的攻略成员
        self.chess_list = self.get_chess()# 棋子资料
        self.equip_list = self.get_equip()# 装备资料
        self.race_list = self.get_race()# 羁绊资料
        self.job_list = self.get_job()  # 职业资料
    def maopao(self,list):  # 冒泡排序
        length = len(list)
        for i in range(length - 1):
            for j in range(length - i - 1):
                if int(list[j]['sortID']) > int(list[j + 1]['sortID']):
                    list[j], list[j + 1] = list[j + 1], list[j]
                    #print(list[j]['detail'])
        return list[::-1]#列表倒置
    def maopao2(self,list):  # 冒泡排序
        length = len(list)
        for i in range(length - 1):
            for j in range(length - i - 1):

                if int(list[j]['price']) > int(list[j + 1]['price']):
                    list[j], list[j + 1] = list[j + 1], list[j]

                    #print(list[j]['detail'])


        return list#列表倒置
    def guolv(self,l):
        list2=[]
        for item in l:
            if item["price"] == '0' or item["price"] == None or item["price"] == "":
                continue
            else:
                list2.append(item)
        return list2  # 列表倒置
    def get_strategy_list(self,mode="赛博之城"):#返回最新卡组列表包括攻略详情
        '''
        返回所有最新卡组的数据列表
        :return:
        '''
        if mode=="赛博之城":
            res =requests.get(f"http://game.gtimg.cn/images/lol/act/tftzlkauto/json/lineupJson/{self.saiji}/6/lineup_detail_total.json",
                              headers=self.headers,verify=False)
        else:
            res = requests.get(
                f"http://game.gtimg.cn/images/lol/act/tftzlkauto/json/lineupJson/s13/62/lineup_detail_total.json.json",
                headers=self.headers, verify=False)
        __data_list=self.maopao(json.loads(res.text)['lineup_list'])

        for i,item in enumerate(__data_list):
            __data_list[i]["detail"]=json.loads(item["detail"].replace("\n", "").replace("\r\n", ""), strict=False)

        return __data_list
    def get_chess(self,mode="赛博之城"):#f获取所以棋子的资料，返回一个列表

        if mode == "赛博之城":

            res=requests.get(f"http://game.gtimg.cn/images/lol/act/img/tft/js/chess.js",headers=self.headers, verify=False)
        else:
            res = requests.get(f"http://game.gtimg.cn/images/lol/act/img/tft/js/15.2-2025.S13-6100/chess-6100.js", headers=self.headers,
                               verify=False)
        j=json.loads(res.text)
        j=j['data']
        j = self.maopao2(j)
        j = self.guolv(j)
        return j
    def get_equip(self,mode="赛博之城"):#获取装资料返回一个列表

        if mode == "赛博之城":
            res=requests.get(f"http://game.gtimg.cn/images/lol/act/img/tft/js/equip.js",headers=self.headers, verify=False)
        else:

            res = requests.get(f"http://game.gtimg.cn/images/lol/act/img/tft/js/15.2-2025.S13-6100/equip-6100.js", headers=self.headers,
                               verify=False)
        j=json.loads(res.text)
        j=j['data']
        equip=[]


        # 排除项
        for XH,i in enumerate(j):

            #排除之前版本删除的装备
            #小件装备为89-97
            # if XH<78 :
            #     continue
            # 排除之前版本删除的装备  Form_zb.py  在这里改
            if i['isShow']=="0":
                continue
            equip.append(i)
        return equip
    def get_job(self,mode="赛博之城"):#获取所有的职业 返回一个列表
        if mode == "赛博之城":
            res=requests.get(f'http://game.gtimg.cn/images/lol/act/img/tft/js/job.js',headers=self.headers, verify=False)
        else:
            res = requests.get(f'http://game.gtimg.cn/images/lol/act/img/tft/js/15.2-2025.S13-6100/job-6100.js', headers=self.headers,
                               verify=False)
        j=json.loads(res.text)#兼容模式
        j=j['data']
        return j
    def get_race(self,mode="赛博之城"):#获取所有的羁绊种族 返回一个列表
        if mode == "赛博之城":
            res=requests.get(f'http://game.gtimg.cn/images/lol/act/img/tft/js/race.js',headers=self.headers, verify=False)
        else:
            res = requests.get(f'http://game.gtimg.cn/images/lol/act/img/tft/js/15.2-2025.S13-6100/race-6100.js', headers=self.headers,
                               verify=False)
        j=json.loads(res.text)
        j=j['data']
        return j
    def chess_id_to_name(self,id=0):
        '''
        棋子id转名字
        :return:
        '''
        id=str(id)
        for item in self.chess_list:
            if item["chessId"]==id:
                return item["displayName"]
tft = TFT()
if __name__=="__main__":
    tft = TFT()
    tft_hexstr = '0231d31431331230f31571a30b316000TFTSet14'
    id_list = tft_decrypt_to_list(tft_hexstr)
    tft_str = tft_encrypt_to_str(id_list)
    name_list = [tft.chess_id_to_name(item) for item in id_list]
    print("解密:", id_list, "=", name_list, "\n加密:", tft_str, "\n测试结果:", tft_str == tft_hexstr)
    #print(tft.strategy_list)10348
    #[print(tft.strategy_list[x]["line_name"]) for x in range(len(tft.strategy_list))]


